#include "IWFFileParser.h"
#include "GraphicsController.h"
#include "..\Libs\libIWF\libIWF.h"
#include "..\Libs\libIWF\iwfFile.h"
#include "..\Libs\libIWF\iwfObjects.h"
#include "StringConverter.h"
#include <stdexcept>


///////////////////////////////////////////////////////////////////////////////

IWFFileParser::IWFFileParser(CGraphicsController& graphicsController,
                             const CFileIWF& sceneFile)
      : m_graphicsController(graphicsController),
      m_sceneFile(sceneFile)
{
}

///////////////////////////////////////////////////////////////////////////////

void IWFFileParser::processTextures(std::vector<IDirect3DTexture9*>& textures)
{
   for (UINT i = 0; i < m_sceneFile.m_vpTextureList.size(); i++)
   {
      iwfTexture* fileTexture = m_sceneFile.m_vpTextureList[i];
      if (fileTexture->TextureSource != TEXTURE_EXTERNAL) { continue; }

      try 
      {
         IDirect3DTexture9* texture = m_graphicsController.loadTextureFromFile(toUnicode(fileTexture->Name));
         textures.push_back(texture);
      }
      catch(std::exception& ex)
      {
      }
   }
}

///////////////////////////////////////////////////////////////////////////////

void IWFFileParser::processMaterials(std::vector<D3DMATERIAL9>& materials)
{
   for (UINT i = 0; i < m_sceneFile.m_vpMaterialList.size(); i++)
   {
      iwfMaterial* material = m_sceneFile.m_vpMaterialList[i];

      D3DMATERIAL9 d3dMat;
      d3dMat.Ambient.a = material->Ambient.a; 
      d3dMat.Ambient.r = material->Ambient.r; 
      d3dMat.Ambient.g = material->Ambient.g; 
      d3dMat.Ambient.b = material->Ambient.b;

      d3dMat.Diffuse.a = material->Diffuse.a;
      d3dMat.Diffuse.r = material->Diffuse.r;
      d3dMat.Diffuse.g = material->Diffuse.g;
      d3dMat.Diffuse.b = material->Diffuse.b;

      d3dMat.Specular.a = material->Specular.a;
      d3dMat.Specular.r = material->Specular.r;
      d3dMat.Specular.g = material->Specular.g;
      d3dMat.Specular.b = material->Specular.b;

      d3dMat.Emissive.a = material->Emissive.a;
      d3dMat.Emissive.r = material->Emissive.r;
      d3dMat.Emissive.g = material->Emissive.g;
      d3dMat.Emissive.b = material->Emissive.b;

      d3dMat.Power = material->Power;

      materials.push_back(d3dMat);
   }
}

///////////////////////////////////////////////////////////////////////////////
